<?php
namespace Tongcheng\Controller;
use Think\Controller;
/*
 * 拼团失败24小时自动退款
 */
class RefundController extends Controller {
   //一个平台很多小程序，退款证书不同。要把证书路径传过去
    public function index(){

        $list = $this->getRefundList();
        $this->refund($list);
    }
    /*
     * 查出拼团失败需要退款的订单
     */
    protected function getRefundList()
    {
        $star = date("Y-m-d H:i:s",strtotime("-2 days"));
        $_24hours = date("Y-m-d H:i:s",strtotime("-1 days"));
        $map = [];
        $map['is_tuan'] = 1;
        $map['status'] = 0;
        $map['is_pay'] = 1;
        $map['create_time'] = array('between', array($star, $_24hours));
        $list = M('order')->where($map)->select();
        return $list;
    }

    /*
     * 根据订单查出平台并退款
     * @param array $list 退款订单数据
     */
    protected function refund($list)
    {
        //循环查出平台并退款
        foreach($list as $v){
            $map['wid'] = $v['wid'];//平台id
            $info = M('wechat')->where($map)->find();
            //未上传证书无法退款
            if(!$info['certificate_path_key'] || !$info['certificate_path_cert']){
                //拼装数据存入表内
                $data['msg'] = "未上传证书";
                $data['status'] = 0;
            }
            //退款金额单位为分，最小为1元
            elseif($v['price'] < 1){
                $data['msg'] = "退款金额小于1元";
                $data['status'] = 0;
                //无法退款信息过滤掉
                M('order')->where(['id'=>$v['id']])->setField('status','3');
            }else{
                //有可以退款
                $info['order'] = $v;
                $result = $this->starRefund($info);
                if($result['result_code'] =="FAIL"){
                    //退款失败
                    $data['status'] = 0;
                    $data['msg'] = $result['err_code_des'];//错误描述
                }else{
                    //退款成功
                    $data['status'] = 1;
                    $data['msg'] = $result['err_code_des'];
                    $data['transaction_id'] = $result['transaction_id'];    //微信订单号
                    $data['out_trade_no'] = $result['out_trade_no'];        //商户订单号
                    $data['out_refund_no'] = $result['out_refund_no'];      //商户退款单号
                    $data['refund_id'] = $result['refund_id'];              //微信退款单号
                    $data['refund_fee'] = $result['refund_fee'];            //退款金额

                    //退款成功更新订单状态不让再次退款
                    M('order')->where(['id'=>$v['id']])->setField('status','3');
                }
            }
            $data['orderid'] = $v['id'];//订单id
            $data['time'] = date("Y-m-d H:i:s",time());
            $data['wid'] = $v['wid'];
            M('refund')->add($data);
        }
    }

    /*
     * 开始退款，调接口
     * @param array $info 退款信息
     */
    protected function starRefund($info)
    {
        //退钱
        vendor('Weixinpay.WxPayJsApiPay');
        //查询订单,根据订单里边的数据进行退款
        $refund = 'refund'.date('YmdHis', time()).rand(1000, 9999);
        $merchid = $info['mch_id']; //商户id

        $input = new \WxPayRefund();
        $input->SetCert_path($info['certificate_path_cert']);//cert证书路径
        $input->SetKey_path($info['certificate_path_key']);//key证书路径
        $input->SetOut_trade_no($info['order']['out_trade_no']);			//自己的订单号
        $input->SetTransaction_id($info['order']['transaction_id']);  	//微信官方生成的订单流水号，在支付成功中有返回
        $input->SetOut_refund_no($refund);			//退款单号
        $input->SetTotal_fee(intval($info['order']['price'])*100);			//订单标价金额，单位为分
        $input->SetRefund_fee(intval($info['order']['price'])*100);			//退款总金额，订单总金额，单位为分，只能为整数
        $input->SetOp_user_id($merchid);
        //$input->SetAppid("wx91169743893f29bf");
        $result = \WxPayApi::refund($input);	//退款操作、
        return $result;
    }
}